package com.github.pig.admin.controller.notificationInfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.zhgl.notification.NotificationUser;
import com.github.pig.admin.service.notificationInfo.NotificationUserService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动用户关联表 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-17
 */
@RestController
@RequestMapping("/notificationUser")
public class NotificationUserController extends BaseController {
    @Autowired private NotificationUserService notificationUserService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return NotificationUser
    */
    @GetMapping("/{id}")
    public R<NotificationUser> get(@PathVariable Integer id) {
        return new R<>(notificationUserService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return notificationUserService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  notificationUser  实体
     * @return success/false
     */
    @PostMapping
    public AjaxJson add(@RequestBody NotificationUser notificationUser) {
        AjaxJson json = new AjaxJson();
        EntityWrapper<NotificationUser> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("create_by",notificationUser.getCreateBy());
        entityWrapper.eq("notification_id",notificationUser.getNotificationId());
        entityWrapper.eq("del_flag","0");
        List<NotificationUser> list = notificationUserService.selectList(entityWrapper);
        if (list.size() > 0){
            json.setSuccess(false);
            json.setMsg("您已报名！");
        }else {
            notificationUser.setCreateDate(new Date());
            notificationUserService.insert(notificationUser);
            json.setSuccess(true);
            json.setMsg("报名成功！");
        }
        return json;
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        NotificationUser notificationUser = new NotificationUser();
        notificationUser.setId(id);
        notificationUser.setUpdateDate(new Date());
        notificationUser.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(notificationUserService.updateById(notificationUser));
    }

    /**
     * 编辑
     * @param  notificationUser  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody NotificationUser notificationUser) {
        notificationUser.setUpdateDate(new Date());
        return new R<>(notificationUserService.updateById(notificationUser));
    }
}
